﻿Imports System.ComponentModel

<ToolboxBitmap(GetType(System.Windows.Forms.ComboBox)), DefaultEvent("SelectedIndexChanged")> _
Public Class LABoxCtrl

#Region "Properties"

    Private _ConnectionString As String
    <Browsable(True), DefaultValue(""), Description("Connection string")> _
    Public Property zConnectionString() As String
        Get
            Return _ConnectionString
        End Get
        Set(ByVal value As String)
            _ConnectionString = value
            Connect()
        End Set
    End Property

    <Browsable(True), DefaultValue(""), Description("LA Box text")> _
    Public Property zText() As String
        Get
            Return TheBox.Text
        End Get
        Set(ByVal value As String)
            TheBox.Text = value
        End Set
    End Property

    <Browsable(True), DefaultValue(0), Description("LA Box current selected LA_Code")> _
    Public Property zSelectedLA_Code() As String

        Get
            Return TheBox.SelectedValue
        End Get
        Set(ByVal value As String)
            TheBox.SelectedValue = value
        End Set
    End Property

    <Browsable(True), DefaultValue(0), Description("LA Box current selected Index")> _
    Public Property SelectedIndex() As Integer

        Get
            Return TheBox.SelectedIndex
        End Get
        Set(ByVal value As Integer)
            TheBox.SelectedIndex = value
        End Set
    End Property

    <Browsable(False), DefaultValue(""), Description("LA Box current selected LA_Name")> _
    Public ReadOnly Property zSelectedLA_Name() As String
        Get
            Return DirectCast(TheBox.SelectedItem, DataRowView).Item("LA_Name")
        End Get
    End Property

    Private _AllOptionAvailable As Boolean
    <Browsable(True), DefaultValue(False), Description("LA Box <All> option available")> _
    Public Property zAllOptionAvailable() As Boolean
        Get
            Return _AllOptionAvailable
        End Get
        Set(ByVal value As Boolean)
            _AllOptionAvailable = value
            Connect()
        End Set
    End Property


    Private _TerritoryCodeFilter As String
    <Browsable(True), DefaultValue(""), Description("Territory Code Filter.  If it is empty string then no filter")> _
    Public Property zTerritoryCodeFilter() As String
        Get
            Return _TerritoryCodeFilter
        End Get
        Set(ByVal value As String)
            _TerritoryCodeFilter = value
            Connect()
        End Set
    End Property


#End Region

#Region "Events"

    Public Custom Event SelectedIndexChanged As EventHandler
        AddHandler(ByVal value As EventHandler)
            AddHandler _TheBox.SelectedIndexChanged, value

        End AddHandler

        RemoveHandler(ByVal value As EventHandler)
            RemoveHandler _TheBox.SelectedIndexChanged, value

        End RemoveHandler

        RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs)

        End RaiseEvent
    End Event

#End Region

#Region "Functions"

    Private Function Connect() As Boolean

        If _ConnectionString = "" Then
            Return False
        End If

        Dim sql As String = ""

        If _AllOptionAvailable = True Then
            sql = "select '' as LA_Code, '<All>' as LA_Name UNION "
        End If
        sql = sql & "select LA_Code, LA_Name from dbo.Local_Authorities "

        If _TerritoryCodeFilter <> "" Then
            sql = sql & "where Region_Code in (select Region_Code from TerritoryJoin where Territory_Code = '" & _TerritoryCodeFilter & "') "
        End If

        sql = sql & "order by LA_Name"

        Dim success As Boolean = True
        Dim dv As DataView

        dv = LoadDataView(sql, _ConnectionString, success)

        If success Then
            ' TheBox.Items.Clear()
            TheBox.DisplayMember = "LA_Name"
            TheBox.ValueMember = "LA_Code"
            TheBox.DataSource = dv
            Return True
        Else
            Return False
        End If

    End Function

#End Region

End Class
